package wortubung.util;

import static wortubung.util.CommonUtils.*;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;

public class ScraperUtils {

	/**
	 * @param trim          if true, trim eacn line
	 * @param markerToStart if not null, start from this pattern (inclusive)
	 * @param markerToStop  if not null, stop at this pattern (exclusive)
	 */
	public static String readFromURL(String urlToRead, boolean trim, String markerToStart, String markerToStop) throws IOException {
		BufferedReader br = null;
		try {
			HttpURLConnection conn = (HttpURLConnection) new URL(urlToRead).openConnection();
			conn.setRequestMethod("GET");

			InputStream is = conn.getInputStream();
			String contentType = conn.getContentType();
			String charset = "UTF-8";
			int idx = contentType.indexOf("charset=");
			if (idx > 0) {
				String tmp = contentType.substring(idx + "charset=".length());
				if (isNotEmpty(tmp))
					charset = tmp.trim();
			}
			br = new BufferedReader(new InputStreamReader(is, Charset.forName(charset)));
			return preRead(br, trim, markerToStart, markerToStop);
		} finally {
			try { br.close(); } catch(Exception e) {}
		}
	}
	
	/**
	 * This is for testing.
	 * 
	 * @param filename      name of the downloaded file, expected in UTF-8 encoding. 
	 * @param trim          if true, trim eacn line
	 * @param markerToStart if not null, start from this pattern (inclusive)
	 * @param markerToStop  if not null, stop at this pattern (exclusive)
	 */
	public static String readFromFile(String filename, boolean trim, String markerToStart, String markerToStop) throws IOException {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF-8"));
			return preRead(br, trim, markerToStart, markerToStop);
		} finally {
			try { br.close(); } catch(Exception e) {}
		}
	}

	public static String preRead(BufferedReader br, boolean trim, String markerToStart, String markerToStop) throws IOException {
		StringBuilder sb = new StringBuilder();

		boolean started = isEmpty(markerToStart);
		boolean stopSooner = isNotEmpty(markerToStop);
		int idx;
		String line;
		boolean isDone = false;
		while (!isDone && (line = br.readLine()) != null) {
			if (!started) {
				idx = line.indexOf(markerToStart);
				if (idx < 0)
					continue;
				line = line.substring(idx);
				started = true;
			}
			else if (stopSooner) {
				idx = line.indexOf(markerToStop);
				if (idx >= 0) {
					line = line.substring(idx);
					isDone = true;
				}
			}
			if (!trim || isNotEmpty(line))
				sb.append(trim ? line.trim() : line).append('\n');
		}

		return sb.toString();
	}

	public static void collectTextToEOL(String content, int startIndex, StringBuilder sb) {
		boolean inTag = false;
		for (int ptr=startIndex; ptr < content.length(); ++ptr) {
			char ch = content.charAt(ptr);
			if (ch == '\n')
				break;
			if (inTag) {
				if (ch != '>')
					continue;
				inTag = false;
			} else {
				if (ch == '<')
					inTag = true;
				else
					sb.append(ch);
			}
		}
	}

	public static void collectTextTo(String content, int ptr, int endIndex, StringBuilder sb) {
		if (endIndex < 0)
			endIndex = content.length();
		boolean inTag = false;
		for (; ptr < endIndex; ++ptr) {
			char ch = content.charAt(ptr);
			if (ch == '\n')
				break;
			if (inTag) {
				if (ch != '>')
					continue;
				inTag = false;
			} else {
				if (ch == '<')
					inTag = true;
				else
					sb.append(ch);
			}
		}
	}

	public static ArrayList<Integer> getIndicesFor(String content, String tag, int startIdx) {
		int idx = content.indexOf(tag, startIdx);
		if (idx < 0)
			return null;

		ArrayList<Integer> indices = new ArrayList<Integer>();
    	indices.add(idx);
    	for (;;) {
    		idx += tag.length();
    		idx = content.indexOf(tag, idx);
    		if (idx < 0)
    			break;
   			indices.add(idx);
    	}
    	return indices;
	}

}
